package qq_clinet.src.com.hzk.qquser_service;

import qq_clinet.src.com.hzk.qq_common.message;
import qq_clinet.src.com.hzk.qq_common.messagetype;
import qq_clinet.src.com.hzk.qq_common.user;
import qq_clinet.src.com.hzk.typeofmessage.useridlist;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @auoter:jackh
 */

public class service {
    private useridlist useridlist=new useridlist();
    private user u=new user();
    private Socket socket;
    public  boolean checkueser(String userid,String psw) throws IOException, ClassNotFoundException {
        u.setUserid(userid);
        u.setPasswd(psw);
        try {
            socket=new Socket(InetAddress.getByName("10.23.118.106"),9999);//发送输出流到本地的ip，服务器端
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            objectOutputStream.writeObject(u);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //服务器接收到user的对象的时候就像客户端发送一个message对象；
        //这里直接利用输入流进行接收
        ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
        message msg= (message) objectInputStream.readObject();
        if (msg.getMessagetype().equals(messagetype.message_login_succed)){//改变信息里面的某一个属性（字符串类型的，这里调用的是接口中的常量）登陆成功
           //这里需要创建一个线程去维护建立的socket连接

            clinetconnectserverThread clinetconnectserverThread = new clinetconnectserverThread(socket,u);
            clinetconnectserverThread.start();
            //放在集合里面。
            manageclinetconnectserverThread.add(userid,clinetconnectserverThread);
            return true;
        }
        else{
            socket.close();
            return false;
        }
    }
   /* public Vector userlist(){
        Vector vector=null;
        try {
            ObjectOutputStream objectOutputStream =new ObjectOutputStream(socket.getOutputStream());
            objectOutputStream.writeObject(useridlist);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            ObjectInputStream objectInputStream=new ObjectInputStream(socket.getInputStream());
            vector=(Vector) objectInputStream.readObject();


        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return vector;
    }*/
    public void userlist() {
        message message=new message();
        message.setMessagetype(messagetype.message_get_online_list);
        try {
            ObjectOutputStream objectOutputStream =new ObjectOutputStream(manageclinetconnectserverThread.getThread(u.getUserid()).getSocket().getOutputStream());
            objectOutputStream.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void exitsend(){
        message message=new message();
        message.setMessagetype(messagetype.exit_get);
        try {
            ObjectOutputStream objectOutputStream =new ObjectOutputStream(manageclinetconnectserverThread.getThread(u.getUserid()).getSocket().getOutputStream());
            objectOutputStream.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void si_talk(String userid,String talkabout){//私聊
        message message=new message();
        message.setMessagetype(messagetype.si_talk_get);
        message.setContent(talkabout);
        message.setGetter(userid);
        message.setSender(u.getUserid());
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            objectOutputStream.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void talk_all_about(String about){
        message message=new message();
        message.setMessagetype(messagetype.talk_all_get);
        message.setContent(about);
        message.setSender(u.getUserid());
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            objectOutputStream.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
